import time

import allure
from selenium.webdriver.common.by import By

from advanced.page_objects.base_page import BasePage
from advanced.utils.log_utils import logger


class CategoryListPage(BasePage):

    __BTN_ADD = (By.XPATH, "//*[text()='添加']")
    __FINDS_ADD = (By.XPATH, '//*[text()="新增商品测试7"]')
    __MSG_DELETE_OPERATE = (By.XPATH, '//*[text()="删除商品测试1"]')

    """商品上架页面：点击添加"""
    @allure.step("商品列表添加按钮")
    def click_add(self):
        logger.info("点击添加按钮")
        # 点击“添加”按钮
        self.do_find(self.__BTN_ADD).click()

        # ==》创建商品页面
        from advanced.page_objects.categroy_create_page import CategoryCreatePage
        return CategoryCreatePage(self.driver)

    """商品列表页面：获取操作结果"""

    @allure.step("获取添加成功的信息")
    def get_operate_result(self):
        # 获取冒泡消息文本
        # res = self.driver.find_elements(By.XPATH, '//*[text()="新增商品测试1"]')
        time.sleep(2)
        res = self.do_finds(self.__FINDS_ADD)
        logger.debug(f"断言获取的实际结果为{res}")
        return res

        # element = WebDriverWait(self.driver, 10).until(expected_conditions.visibility_of_element_located(
        #     (By.XPATH, '//p[conditions(text(), "创建")]')))

        # ==》返回消息文本
        # return element.text

    @allure.step("删除创建的商品")
    def delete_category(self, category_name):
        time.sleep(2)
        # 对指定商品进行删除
        self.do_find((By.XPATH, f"//*[text()='{category_name}']/../..//*[text()='删除']")).click()
        time.sleep(2)
        # ==>跳转到当前页面
        return CategoryListPage(self.driver)

    @allure.step("获取删除的商品信息")
    def get_delete_result(self):
        # 获取冒泡消息文本
        time.sleep(2)
        res = self.do_finds(self.__MSG_DELETE_OPERATE)
        logger.debug(f"断言获取的实际结果为{res}")
        return res
